﻿using System.IO;
using mitasoft.Common.FtpHelpers;
using System;
using mitasoft.Common.Constants;

namespace mitasoft.Commands
{
    public static class MediaCommands
    {
        public static void CopyMediaFromFtp(string ftpUser, string ftpPass)
        {
            string rootFtp = "ftp://ftp.moldovanu.net/Media/Windows-Live-Writer/";
            string runDate = DateTime.Now.ToString("DD-mm-yyyy");

            string [] wlwMediaDirs = Helpers.ListContent(ftpUser, ftpPass, rootFtp);

            foreach (var subDir in wlwMediaDirs)
            {
                string subDirPath = rootFtp + subDir;
                string [] wlwMediaSubDirs = Helpers.ListContent(ftpUser, ftpPass, subDirPath);

                foreach (var file in wlwMediaSubDirs)
                {
                    string ftpFilePath = string.Format("{0}/{1}", subDirPath, file);

                    // path is relative to running dir (tool)
                    string diskDirPath = Path.Combine(Paths.BACKUP_TOOL_FOLDER, runDate, subDir);
                    string diskFilePath = Path.Combine(diskDirPath, file);

                    if (!Directory.Exists(diskDirPath))
                    {
                        Directory.CreateDirectory(diskDirPath);
                    }

                    if (File.Exists(diskFilePath))
                    {
                        File.Delete(diskFilePath);
                    }

                    Helpers.CopyFromFtp(ftpUser, ftpPass, ftpFilePath, diskFilePath);
                }
            }
        }
    }
}
